package com.xd.server.student.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xd.common.response.ResponseObject;
import com.xd.common.response.ResponseResult;
import com.xd.server.student.dto.StudentDTO;
import com.xd.server.student.service.IStudentService;
import com.xd.server.student.vo.StudentVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.hibernate.validator.constraints.Length;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Student)表控制层
 *
 * @author XD
 * @since 2023-10-10 09:04:05
 */
@Api(value = "com.xd.server.student.controller.StudentController", tags = {"管理"})
@RestController
@RequestMapping("student")
public class StudentController {
    /**
     * 服务对象
     */
    @Resource
    private IStudentService studentService;

    /**
     * 保存一个
     *
     * @param studentDTO 传输类DTO
     * @returns com.xd.common.response.ResponseObject
     */
    @PostMapping("/save-or-update")
    @ApiOperation(value = "保存", notes = "保存")
    public ResponseObject saveOrUpdate(@RequestBody @Validated StudentDTO studentDTO) {
        int i = studentService.saveOrUpdateStudent(studentDTO);
        if (i < 0) {
            return ResponseResult.error("操作失败");
        }
        return ResponseResult.success();
    }

    /**
     * 删除一个
     *
     * @param id id主键
     * @return com.xd.common.response.ResponseObject
     */
    @GetMapping("delete")
    @ApiOperation(value = "删除", notes = "根据主键删除")
    public ResponseObject delete(@ApiParam(value = "主键", name = "id", required = true, example = "1")
                                 @Length(message = "id不得超过64位", min = 1, max = 64)
                                 @RequestParam("id") String id) {
        int i = studentService.deleteStudent(id);
        if (i < 0) {
            return ResponseResult.error("操作失败");
        }
        return ResponseResult.success();
    }


    /**
     * 查询一个
     *
     * @param id 主键
     * @return com.xd.common.response.ResponseObject
     */
    @GetMapping("findOne")
    @ApiOperation(value = "查询一个", notes = "根据主键查询")
    public ResponseObject<StudentVO> SelectOne(@ApiParam(name = "id", value = "主键", required = true, example = "1")
                                               @Length(message = "id长度不得超过64位", min = 1, max = 64)
                                               @RequestParam(value = "id") String id) {

        return ResponseResult.success(studentService.findStudent(id));
    }

    /**
     * 查询所有
     *
     * @return com.xd.common.response.ResponseObject
     */
    @GetMapping("findAll")
    @ApiOperation(value = "查询所有", notes = "查询所有")
    public ResponseObject<List<StudentVO>> SelectAll() {
        List<StudentVO> allStudent = studentService.findAllStudent();
        return ResponseResult.success(allStudent.size(), allStudent);
    }

    /**
     * 查询所有-分页
     *
     * @return com.xd.common.response.ResponseObject
     */
    @GetMapping("findPage")
    @ApiOperation(value = "查询所有-分页", notes = "查询所有-分页")
    public ResponseObject<List<StudentVO>> findPage(@ApiParam StudentDTO studentDTO) {
        Page<StudentVO> page = studentService.findPageStudent(studentDTO);
        return ResponseResult.success(page.getTotal(), page.getRecords());
    }

}
